﻿@{
    Layout = null;
    ViewBag.Title = "Uploader";
}
<link href="/Scripts/uploadify/uploadify.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/uploadify/swfobject.js" type="text/javascript"></script>
<script src="/Scripts/uploadify/jquery.uploadify.v2.1.4.modify.js" type="text/javascript"></script>
<script type="text/javascript">
    //    $(function () {
    //        $('#Common_UploaderFile_UpLoader_Form_FileLoad').uploadify({
    //            'uploader': '/Scripts/uploadify/uploadify.swf?id=' + (new Date()).getTime(),
    //            'script': '@ViewBag.Path' + "/Home/Upload", //'/Common/UploaderFile/UploadFile', // + FileUrl
    //            'cancelImg': '/Scripts/uploadify/cancel.png',
    //            'queueID': 'Common_UploaderFile_AddFile_fileQueue',
    //            'wmode': 'transparent',
    //            'buttonImg': '/Scripts/uploadify/button.gif',
    //            'multi': true,
    //            'method': 'Post',
    //            'auto': true,
    //            'sizeLimit': '2048000000',
    //            //            'queueSizeLimit':5,
    //            'removeCompleted': false,
    //            'scriptData': { "dirPath": escape("@ViewBag.FilePath") },
    //            'onComplete': function (event, queueID, fileObj, response, data) {
    //            },
    //            'onError': function (event, queueID, fileObj, errorObj) {
    //                alertMsg.warn(errorObj.type + "Error:" + errorObj.info);
    //            }
    //        });
    //    });
    var settings = {
        MoudleID: "",
        ToSelectIDs: new Array(),
        ToDeleteIDs: new Array(),
        BizDeleteURL: "",
        BizSaveUrl: "",
        BizType: "",
        BizID: "",
        isDelete: true,
        dirWhere:""
    };
    var direvtoryID;
    var UploaderFileIndexJS = function () {

        function fileListLoad(flag) {
            debugger;
            $("#FileList").html("").load("/Common/UploaderFile/FileList/?DirevtoryID=" + direvtoryID + "&Ids=" + settings.ToSelectIDs + "&DeleteIds=" + settings.ToDeleteIDs + "&isDelete=" + settings.isDelete, function () {
                if (!settings.isDelete && flag) { //控制不能上传
                    $("#Common_UploaderFile_AddFile_fileQueue").hide();
                    $("#Common_UploaderFile_Uploader_Content .uploadContent, #FileList .uploadBox").hide();
                    //$("#FileList object").hide();
                    $("#Common_UploaderFile_UpLoader_fileTbody a[title='删除']").hide();
                } else if (flag) {
                    $("#Common_UploaderFile_AddFile_fileQueue").hide();
                    $("#Common_UploaderFile_Uploader_Content .uploadContent,#FileList .uploadBox").hide();
                }
                else {
                    $("#Common_UploaderFile_AddFile_fileQueue").show();
                    $("#Common_UploaderFile_Uploader_Content .uploadContent,#FileList .uploadBox").show();
                    //$("#FileList object").show();
                }
            });
        }
        //文件分类数据绑定
        function _BindZTree() {
            var zTree_Common_UploaderFile_Uploader = $("#Common_UploaderFile_Uploader_tree").zTree({
                checkable: false,
                async: true,
                isSimpleData: true,
                editable: false,
                asyncUrl: "/Common/UploaderFile/GetDirectory?MoudleID=" + settings.MoudleID + "&Ids=" + settings.ToSelectIDs + "&dirWhere=" + encodeURIComponent(settings.dirWhere),  //获取节点数据的URL地址
                nameCol: "DIRECTORY_NAME",
                treeNodeKey: "DIRECTORY_ID",
                treeNodeParentKey: "DIRECTORY_PARENT",
                callback: {
                    asyncSuccess: function (event, treeId, treeNode, msg) {
                        zTree_Common_UploaderFile_Uploader.expandAll();
                        direvtoryID = 0;
                        fileListLoad(true);
                        //                        var treeNodes = JSON.parse(msg);
                        //                        if (treeNodes != null) {
                        //                            var nodes;
                        //                            for (var i = 0; i < treeNodes.length; i++) {
                        //                                nodes = zTree_Common_UploaderFile_Uploader.getNodeByParam("DIRECTORY_PARENT", treeNodes[i].DIRECTORY_ID);
                        //                                if (nodes == null) {
                        //                                    direvtoryID = treeNodes[i].DIRECTORY_ID;
                        //                                    zTree_Common_UploaderFile_Uploader.selectNode(zTree_Common_UploaderFile_Uploader.getNodeByParam("DIRECTORY_ID", treeNodes[i].DIRECTORY_ID));
                        //                                    fileListLoad();
                        //                                    break;
                        //                                }
                        //                            }
                        //                        }
                    },
                    click: function (event, treeId, treeNode) {
                        direvtoryID = treeNode.DIRECTORY_ID;
                        if (treeNode.DIRECTORY_ID > 0) {
                            fileListLoad(false);
                        }
                    }
                }
            }, []);
        }

        //删除事件绑定

        //保存事件绑定
        function _BindSava() {
            //保存按钮点击事件
            $("#Common_UploaderFile_AddFile_SaveBtn").bind("click", function () {
                Common_UploaderFile_AddFile_fileNames = "";
                $("#Common_UploaderFile_AddFile_fileQueue").find(".completed").each(function () {
                    var File_File_AddFileItem_item = $(this).find(".fileName").text();
                    Common_UploaderFile_AddFile_fileNames += File_File_AddFileItem_item + ">";
                });
                if (Common_UploaderFile_AddFile_fileNames == "") {
                    alertMsg.info("未上传文件");
                    return;
                }
                $.post("/Common/UploaderFile/SaveFile", { filesNames: Common_UploaderFile_AddFile_fileNames, dirId: direvtoryID }, function (json) {
                    if (json.statusCode == "200") {
                        debugger;
                        var UploaderOption = $("#@ViewBag.UploaderOptionID").attr("uploaderoption"); //获取全局对象字符串
                        var oldArr = settings.ToSelectIDs.join(',');
                        var changedOption = UploaderOption.replace("'ToSelectIDs':[" + oldArr + "]", "'ToSelectIDs':{bizId}");

                        var ids = json.data.substr(0, json.data.length - 1);
                        if (oldArr == "") {
                            changedOption = changedOption.replace("'ToSelectIDs':{bizId}", "'ToSelectIDs':[" + ids + "]");
                        } else {
                            changedOption = changedOption.replace("'ToSelectIDs':{bizId}", "'ToSelectIDs':[" + oldArr + "," + ids + "]");
                        }
                        $.post(settings.BizSaveUrl, { "BizID": settings.BizID, "BizType": settings.BizType, "FileIds": ids }, function (json) {
                            if (json != null && json != "") {
                                if (json.statusCode == "200") {
                                    alertMsg.correct(json.message);
                                    var AddIds = new Array();
                                    AddIds = ids.split(',');
                                    for (var i = 0; i < AddIds.length; i++) {
                                        settings.ToSelectIDs.push(parseInt(AddIds[i]));
                                    }
                                    var newIds = json.data;
                                    oldArr = settings.ToDeleteIDs.join(',');
                                    changedOption = changedOption.replace("'ToDeleteIDs':[" + oldArr + "]", "'ToDeleteIDs':{bizId}");

                                    AddIds = newIds.split(',');
                                    for (var i = 0; i < AddIds.length; i++) {
                                        settings.ToDeleteIDs.push(parseInt(AddIds[i]));
                                    }
                                    if (oldArr == "") {
                                        changedOption = changedOption.replace("'ToDeleteIDs':{bizId}", "'ToDeleteIDs':[" + newIds + "]");
                                    }
                                    else {
                                        changedOption = changedOption.replace("'ToDeleteIDs':{bizId}", "'ToDeleteIDs':[" + oldArr + "," + newIds + "]");
                                    }
                                    $("#@ViewBag.UploaderOptionID").attr("uploaderoption", changedOption);

                                    fileListLoad(true);
                                    $("#Common_UploaderFile_AddFile_fileQueue").html('');
                                } else {
                                    alertMsg.error(json.message);
                                }
                                // $.pdialog.closeCurrent();
                            }
                        });
                    }
                    else {
                        alertMsg.error(json.message);
                    }
                });
            });

        }

        this.init = function () {
            debugger;
            var UploaderOption = $("#@ViewBag.UploaderOptionID").attr("uploaderoption"); //获取全局对象字符串
            var options = eval("(" + UploaderOption + ")"); //转换全局对象
            if (!!UploaderOption) {
                $.extend(settings, options);
            }

            _BindZTree();
            //_BindDelete();
            _BindSava();
        }
    }
    function fun_Delete_Common_FileList(FileID, BizID) {
        if (BizID == 0) {
            alertMsg.error("文件不存在");
        }
        else {
            alertMsg.confirm("确定要继续吗？", {
                okCall: function () {
                    //1.首先post对象参数中的删除方法
                    $.post(settings.BizDeleteURL, { "id": BizID }, function (json) {
                        //DWZ.ajaxDone(json);
                        if (json.statusCode == DWZ.statusCode.ok) {
                            //再次post删除文件
                            $.post("/Common/UploaderFile/Delete", { "id": FileID }, function (json) {
                                DWZ.ajaxDone(json);
                                if (json.statusCode == DWZ.statusCode.ok) {
                                    var UploaderOption = $("#@ViewBag.UploaderOptionID").attr("uploaderoption"); //获取全局对象字符串
                                    var OldDeleteIDs = settings.ToDeleteIDs.join(',');
                                    var changedOption = UploaderOption.replace("'ToDeleteIDs':[" + OldDeleteIDs + "]", "'ToDeleteIDs':{bizId}");

                                    var Deleteindex = settings.ToDeleteIDs.indexOf(BizID);
                                    if (Deleteindex > -1) {
                                        settings.ToDeleteIDs.splice(Deleteindex, 1);
                                    }
                                    var NewDeleteIDs = settings.ToDeleteIDs.join(',');
                                    changedOption = changedOption.replace("'ToDeleteIDs':{bizId}", "'ToDeleteIDs':[" + NewDeleteIDs + "]");

                                    var OldSelectIDs = settings.ToSelectIDs.join(',');
                                    changedOption = changedOption.replace("'ToSelectIDs':[" + OldSelectIDs + "]", "'ToSelectIDs':{bizId}");
                                    var SelectIDsindex = settings.ToSelectIDs.indexOf(FileID);
                                    if (SelectIDsindex > -1) {
                                        settings.ToSelectIDs.splice(SelectIDsindex, 1);
                                    }
                                    var NewSelectIDs = settings.ToSelectIDs.join(',');
                                    changedOption = changedOption.replace("'ToSelectIDs':{bizId}", "'ToSelectIDs':[" + NewSelectIDs + "]");
                                    $("#@ViewBag.UploaderOptionID").attr("uploaderoption", changedOption);

                                    //$("#FileList").html("").load("/Common/UploaderFile/FileList/?DirevtoryID=" + direvtoryID + "&Ids=" + settings.ToSelectIDs + "&DeleteIds=" + settings.ToDeleteIDs, function () {

                                    //initUI($("#FileList"));

                                    if (!settings.isDelete) { //控制不能上传
                                        $("#Common_UploaderFile_AddFile_fileQueue").hide();
                                        $("#Common_UploaderFile_Uploader_Content .uploadContent").hide();
                                        $("#Common_UploaderFile_UpLoader_fileTbody a[title='删除']").hide();
                                    } else {
                                        $("#Common_UploaderFile_AddFile_fileQueue").show();
                                        $("#Common_UploaderFile_Uploader_Content .uploadContent").show();
                                        $("#FileList tr[fielid='" + FileID + "']").remove();
                                    }
                                    //});
                                }
                                else {
                                    alertMsg.error(json.message);
                                }
                            }, "json");
                        }
                        else {
                            alertMsg.error(json.message);
                        }
                    }, "json");
                }
            });
        }
        //});

    }
    //页面加载绑定
    $(document).ready(function () {
        new UploaderFileIndexJS().init();
    });
</script>
<div class="page" layouth="40" id="Common_UploaderFile_Uploader_Container">
    <div id="Common_UploaderFile_Uploader" style="height: 100%; background: #fff;">
        <div id="Common_UploaderFile_Uploader_Uploader" class="leftPanel">
            <ul id="Common_UploaderFile_Uploader_tree" class="ztree">
            </ul>
        </div>
        <div id="Common_UploaderFile_Uploader_Content" class="rightPanel" style="overflow-y: auto;">
            <div class="uploadContent" style="padding-bottom: 0;">
                <div class="uploadBox" style="border-bottom: none;">
                    <div id="Common_UploaderFile_AddFile_fileQueue">
                    </div>
                    @*<input type="text" id="Common_UploaderFile_UpLoader_Form_FileLoad" value="" />*@
                    <div id="Common_UploaderFile_UpLoader_Form_FileQueue">
                    </div>
                </div>
            </div>
            <div style="padding: 0 5px 0 5px;">
                <div id="FileList">
                </div>
            </div>
        </div>
    </div>
</div>
<div class="formBar">
    <ul>
        <li>
            <button type="button" id="Common_UploaderFile_AddFile_SaveBtn" class="btn btn-success btn-sm">
                保存
            </button>
        </li>
        <li>
            <button type="button" class="close btn btn-default btn-sm">
                关闭
            </button>
        </li>
    </ul>
</div>
